package com.wcards.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wcards.service.IncomeInfoService;
import com.wcards.service.UserInfoService;
import com.wcards.dto.IncomeInfoDTO;
import com.wcards.entity.IncomeInfo;
import com.wcards.entity.UserInfo;
import com.wcards.mapper.IncomeInfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 大黑
 */
/*
收入信息表
 */
@Service
public class IncomeInfoServiceImpl extends ServiceImpl<IncomeInfoMapper, IncomeInfo> implements IncomeInfoService {
    @Autowired
    private IncomeInfoService incomeInfoService;
    @Autowired
    private UserInfoService userInfoService;

    //不传参数：全部信息
    //根据传入参数来进行查询
    @Override
    public Page<IncomeInfo> findInformation(IncomeInfoDTO incomeInfoDTO, Integer pageSize, Integer pageNumber) {
        Page<IncomeInfoDTO> pageInfo = new Page<IncomeInfoDTO>(pageSize, pageNumber);
        Page<IncomeInfo> incomeInfoPage = new Page<IncomeInfo>(pageSize, pageNumber);

        if ("\"\"".equals(incomeInfoDTO.getIncomeType())) {
            incomeInfoService.page(incomeInfoPage, null);
            BeanUtils.copyProperties(incomeInfoPage, pageInfo, "userName");
        }
        List<IncomeInfo> incomeInfoList = incomeInfoPage.getRecords();
        List<IncomeInfoDTO> incomeInfoDTOList = pageInfo.getRecords();
        int sum = 0;
        for (IncomeInfo incomeInfo : incomeInfoList) {
            LambdaQueryWrapper<UserInfo> wrapper = new LambdaQueryWrapper<UserInfo>()
                    .eq(UserInfo::getUserId, incomeInfo.getUserId());
            String userName = userInfoService.getOne(wrapper).getUserName();
            incomeInfoDTOList.get(sum).setUserName(userName);
        }
        return incomeInfoService.page(incomeInfoPage, null);
    }
}
